<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="日期" prop="date">
        <el-date-picker
          v-model="formData.date"
          type="date"
          value-format="x"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="国家或地区代码" prop="countryOrRegion">
        <el-input v-model="formData.countryOrRegion" placeholder="请输入国家或地区代码" />
      </el-form-item>
      <el-form-item label="展示次数" prop="impressions">
        <el-input v-model="formData.impressions" placeholder="请输入展示次数" />
      </el-form-item>
      <el-form-item label="点击次数" prop="taps">
        <el-input v-model="formData.taps" placeholder="请输入点击次数" />
      </el-form-item>
      <el-form-item label="总安装次数" prop="installs">
        <el-input v-model="formData.installs" placeholder="请输入总安装次数" />
      </el-form-item>
      <el-form-item label="新安装次数" prop="newDownloads">
        <el-input v-model="formData.newDownloads" placeholder="请输入新安装次数" />
      </el-form-item>
      <el-form-item label="重复安装次数" prop="reDownloads">
        <el-input v-model="formData.reDownloads" placeholder="请输入重复安装次数" />
      </el-form-item>
      <el-form-item label="启用定位安装次数" prop="latOnInstalls">
        <el-input v-model="formData.latOnInstalls" placeholder="请输入启用定位安装次数" />
      </el-form-item>
      <el-form-item label="禁用定位安装次数" prop="latOffInstalls">
        <el-input v-model="formData.latOffInstalls" placeholder="请输入禁用定位安装次数" />
      </el-form-item>
      <el-form-item label="点击率" prop="ttr">
        <el-input v-model="formData.ttr" placeholder="请输入点击率" />
      </el-form-item>
      <el-form-item label="每次转化费用" prop="avgCpaAmount">
        <el-input v-model="formData.avgCpaAmount" placeholder="请输入每次转化费用" />
      </el-form-item>
      <el-form-item label="转化货币" prop="avgCpaCurrency">
        <el-input v-model="formData.avgCpaCurrency" placeholder="请输入转化货币" />
      </el-form-item>
      <el-form-item label="每次点击付费" prop="avgCptAmount">
        <el-input v-model="formData.avgCptAmount" placeholder="请输入每次点击付费" />
      </el-form-item>
      <el-form-item label="点击货币" prop="avgCptCurrency">
        <el-input v-model="formData.avgCptCurrency" placeholder="请输入点击货币" />
      </el-form-item>
      <el-form-item label="每千展费用" prop="avgCpmAmount">
        <el-input v-model="formData.avgCpmAmount" placeholder="请输入每千展费用" />
      </el-form-item>
      <el-form-item label="展示货币" prop="avgCpmCurrency">
        <el-input v-model="formData.avgCpmCurrency" placeholder="请输入展示货币" />
      </el-form-item>
      <el-form-item label="支出金额" prop="localSpendAmount">
        <el-input v-model="formData.localSpendAmount" placeholder="请输入支出金额" />
      </el-form-item>
      <el-form-item label="支出货币" prop="localSpendCurrency">
        <el-input v-model="formData.localSpendCurrency" placeholder="请输入支出货币" />
      </el-form-item>
      <el-form-item label="转化率" prop="conversionRate">
        <el-input v-model="formData.conversionRate" placeholder="请输入转化率" />
      </el-form-item>
      <el-form-item label="广告系列 ID" prop="campaignId">
        <el-input v-model="formData.campaignId" placeholder="请输入广告系列 ID" />
      </el-form-item>
      <el-form-item label="广告系列名称" prop="campaignName">
        <el-input v-model="formData.campaignName" placeholder="请输入广告系列名称" />
      </el-form-item>
      <el-form-item label="是否删除" prop="isDeleted">
        <el-radio-group v-model="formData.isDeleted">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="广告系统状态" prop="campaignStatus">
        <el-radio-group v-model="formData.campaignStatus">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="应用名称" prop="appName">
        <el-input v-model="formData.appName" placeholder="请输入应用名称" />
      </el-form-item>
      <el-form-item label="应用 ID" prop="adamId">
        <el-input v-model="formData.adamId" placeholder="请输入应用 ID" />
      </el-form-item>
      <el-form-item label="状态" prop="servingStatus">
        <el-radio-group v-model="formData.servingStatus">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="国家或地区" prop="countriesOrRegions">
        <el-input v-model="formData.countriesOrRegions" placeholder="请输入国家或地区" />
      </el-form-item>
      <el-form-item label="修改时间" prop="modificationTime">
        <el-date-picker
          v-model="formData.modificationTime"
          type="date"
          value-format="x"
          placeholder="选择修改时间"
        />
      </el-form-item>
      <el-form-item label="日预算金额" prop="dailyBudgetAmount">
        <el-input v-model="formData.dailyBudgetAmount" placeholder="请输入日预算金额" />
      </el-form-item>
      <el-form-item label="日预算货币" prop="dailyBudgetCurrency">
        <el-input v-model="formData.dailyBudgetCurrency" placeholder="请输入日预算货币" />
      </el-form-item>
      <el-form-item label="展示状态" prop="displayStatus">
        <el-radio-group v-model="formData.displayStatus">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="广告展示位置" prop="supplySources">
        <el-select v-model="formData.supplySources" placeholder="请选择广告展示位置">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="广告渠道类型" prop="adChannelType">
        <el-select v-model="formData.adChannelType" placeholder="请选择广告渠道类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="组织 ID" prop="orgId">
        <el-input v-model="formData.orgId" placeholder="请输入组织 ID" />
      </el-form-item>
      <el-form-item label="计费方式" prop="billingEvent">
        <el-input v-model="formData.billingEvent" placeholder="请输入计费方式" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { CampaignDataApi, CampaignDataVO } from '@/api/asasc/campaign'

/** 广告数据 表单 */
defineOptions({ name: 'CampaignDataForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  date: undefined,
  countryOrRegion: undefined,
  impressions: undefined,
  taps: undefined,
  installs: undefined,
  newDownloads: undefined,
  reDownloads: undefined,
  latOnInstalls: undefined,
  latOffInstalls: undefined,
  ttr: undefined,
  avgCpaAmount: undefined,
  avgCpaCurrency: undefined,
  avgCptAmount: undefined,
  avgCptCurrency: undefined,
  avgCpmAmount: undefined,
  avgCpmCurrency: undefined,
  localSpendAmount: undefined,
  localSpendCurrency: undefined,
  conversionRate: undefined,
  campaignId: undefined,
  campaignName: undefined,
  isDeleted: undefined,
  campaignStatus: undefined,
  appName: undefined,
  adamId: undefined,
  servingStatus: undefined,
  countriesOrRegions: undefined,
  modificationTime: undefined,
  dailyBudgetAmount: undefined,
  dailyBudgetCurrency: undefined,
  displayStatus: undefined,
  supplySources: undefined,
  adChannelType: undefined,
  orgId: undefined,
  billingEvent: undefined
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await CampaignDataApi.getCampaignData(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as CampaignDataVO
    if (formType.value === 'create') {
      await CampaignDataApi.createCampaignData(data)
      message.success(t('common.createSuccess'))
    } else {
      await CampaignDataApi.updateCampaignData(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    date: undefined,
    countryOrRegion: undefined,
    impressions: undefined,
    taps: undefined,
    installs: undefined,
    newDownloads: undefined,
    reDownloads: undefined,
    latOnInstalls: undefined,
    latOffInstalls: undefined,
    ttr: undefined,
    avgCpaAmount: undefined,
    avgCpaCurrency: undefined,
    avgCptAmount: undefined,
    avgCptCurrency: undefined,
    avgCpmAmount: undefined,
    avgCpmCurrency: undefined,
    localSpendAmount: undefined,
    localSpendCurrency: undefined,
    conversionRate: undefined,
    campaignId: undefined,
    campaignName: undefined,
    isDeleted: undefined,
    campaignStatus: undefined,
    appName: undefined,
    adamId: undefined,
    servingStatus: undefined,
    countriesOrRegions: undefined,
    modificationTime: undefined,
    dailyBudgetAmount: undefined,
    dailyBudgetCurrency: undefined,
    displayStatus: undefined,
    supplySources: undefined,
    adChannelType: undefined,
    orgId: undefined,
    billingEvent: undefined
  }
  formRef.value?.resetFields()
}
</script>
